<?php
// +----------------------------------------------------------------------
// | 权限群组
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\model\Group as GroupModel;

class Group extends Base {

    private $GroupModel;

    public function _initialize() {
        parent::_initialize();
        $this->GroupModel = new GroupModel;
    }

    /**
     * 列表
     * @access public
     */
    public function index(){
        $this->isAuth('admin.group.index');
        //菜单开始
        $pathMaps = [
            ['name'=>'<i class="iconfont icon-home"></i> '.lang('lang_group_ui_title'),'url'=>'javascript:;']
        ];
        $editMenu = [
            ['name'=>'<i class="iconfont icon-cjsqm"></i> '.lang('lang_ui_add'),'url'=>url('admin/group/add')]
        ];
        $this->assign('pathMaps',json_encode($pathMaps));
        $this->assign('editMenu',json_encode($editMenu));
        //菜单结束
        $lists = $this->GroupModel->select_lists();
        $page  = $lists->render();
        $this->assign('lists',$lists);
        $this->assign('page',$page);
        return $this->fetch(); 
    }
    
    /**
     * 添加权限节点
     * @access public
     */
    public function add() {
        $this->isAuth('admin.group.add');
        if(request()->isAjax()){
            $data = [
                'title'    => input('post.title/s'),
                'auth_ids' => input('post.auth_ids'),
                'about'    => input('post.about/s'),
            ];
            $validate = $this->validate($data,'Group.add');
            if(true !== $validate){
                return json(['code'=>1,'message'=>$validate]);
            }
            $result  = $this->GroupModel->add_data($data);
            if(!$result){
                return json(['code'=>1,'message'=>lang('lang_message_error')]);
            }else{
                return json(['code'=>0,'message'=>lang('lang_message_success'),'data'=>['url'=>url('admin/group/index')]]);
            }
        }else{
            //菜单开始
            $pathMaps = [
                ['name'=>'<i class="iconfont icon-arrowleft"></i> '.lang('lang_ui_back'),'url'=>url('admin/group/index')]
            ];
            $this->assign('pathMaps',json_encode($pathMaps));
            $editMenu = [];
            $this->assign('editMenu',json_encode($editMenu));
            //菜单结束
            return $this->fetch();
        }
    } 
    
    /**
     * 编辑权限节点
     * @access public
     */
    public function edit() {
        $this->isAuth('admin.group.edit');
        if(request()->isAjax()){
            $data = [
                'id'       => input('post.id/s'),
                'title'    => input('post.title/s'),
                'auth_ids' => input('post.auth_ids'),
                'about'    => input('post.about/s'),
            ];
            $validate = $this->validate($data,'Group.edit');
            if(true !== $validate){
                return json(['code'=>1,'message'=>$validate]);
            }
            $result  = $this->GroupModel->edit_data($data);
            if(!$result){
                return json(['code'=>1,'message'=>lang('lang_message_error')]);
            }else{
                return json(['code'=>0,'message'=>lang('lang_message_success'),'data'=>['url'=>url('admin/group/index')]]);
            }
        }else{
            //菜单开始
            $pathMaps = [
                ['name'=>'<i class="iconfont icon-arrowleft"></i> '.lang('lang_ui_back'),'url'=>url('admin/group/index')]
            ];
            $this->assign('pathMaps',json_encode($pathMaps));
            $editMenu = [];
            $this->assign('editMenu',json_encode($editMenu));
            //菜单结束
            $id   = input('id/d');
            $info = $this->GroupModel->find_info($id);
            if($info['auth_ids'] ){
                $info['auth_ids'] = implode(',',json_decode($info['auth_ids'],true));
            }
            $this->assign('info',$info);
            return $this->fetch();
        }
    }
    
    /**
     * 删除
     * @access public
     */
    public function delete() {
        $this->isAuth('admin.group.delete');
        $id        = input('id/d');
        $info =  model('User')->find_user_info(['group_id'=>$id]);
        if($info){
            return json(['code'=>1,'message'=>lang('lang_group_delete')]);
        }
        $result  = $this->GroupModel->delete_data($id);
        if(!$result){
            return json(['code'=>1,'message'=>lang('lang_message_error')]);
        }else{
            return json(['code'=>0,'message'=>lang('lang_message_success')]);
        }
    }
}